#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// 字符雨效果
void matrix_rain(int width, int height) {
    srand(time(0));
    char* screen = (char*)malloc(width * height * sizeof(char));
    int* drops = (int*)malloc(width * sizeof(int));
    
    // 初始化雨滴位置
    for (int i = 0; i < width; i++) {
        drops[i] = rand() % height;
        screen[i] = rand() % 93 + 33; // 随机可打印字符
    }
    
    while (1) {
        // 清屏
        system("cls");
        
        // 绘制字符雨
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                if (y == drops[x]) {
                    screen[x + y * width] = rand() % 93 + 33;
                    printf("%c", screen[x + y * width]);
                    drops[x] = (drops[x] + 1) % height;
                } else if (y < drops[x] && y > drops[x] - 5) {
                    printf("%c", screen[x + y * width]);
                } else {
                    printf(" ");
                }
            }
            printf("\n");
        }
        
        Sleep(50); // 控制速度
    }
    
    free(screen);
    free(drops);
}

// 使用示例：matrix_rain(80, 25);